﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace TestForm
{
    public partial class frmSettings : Form
    {
        private frmMain m_parent;
        public frmSettings(frmMain mainForm)
        {
            m_parent = mainForm;
            InitializeComponent();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch (tvSettings.SelectedNode.Text)
            {
                case "Server":
                    break;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtApiKey.Text != String.Empty && txtPort.Text != String.Empty && txtServer.Text != String.Empty)
            {
                Properties.Settings.Default.apikey = txtApiKey.Text;
                Properties.Settings.Default.server = txtServer.Text;
                Properties.Settings.Default.port = txtPort.Text;
                Properties.Settings.Default.username = txtUsername.Text;
                Properties.Settings.Default.password = txtPass.Text;
                Properties.Settings.Default.nzbAssoc = chkAssoc.Checked;
                try
                {
                    if (int.Parse(txtRefreshRate.Text) < 2)
                    {
                        DialogResult t = MessageBox.Show("Refresh intervals of less than 2 seconds are not recommended unless you're connecting locally. Do you wish to proceed?", "Warning", MessageBoxButtons.YesNo);
                        if (t == DialogResult.No)
                        {
                            return;
                        }
                    }
                    Properties.Settings.Default.refreshrate = (double.Parse(txtRefreshRate.Text) * 1000);
                }
                catch
                {
                    Properties.Settings.Default.refreshrate = 3000;
                }
                Properties.Settings.Default.Save();
                this.Close();
            }
            else
            {
                MessageBox.Show("Server, Port, ApiKey and Refresh Speed must be entered. Refresh Speed is entered in seconds.");
            }
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            txtApiKey.Text = Properties.Settings.Default.apikey;
            txtServer.Text = Properties.Settings.Default.server;
            txtPort.Text = Properties.Settings.Default.port;
            txtUsername.Text = Properties.Settings.Default.username;
            txtPass.Text = Properties.Settings.Default.password;
            txtRefreshRate.Text = (Properties.Settings.Default.refreshrate / 1000).ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chkAssoc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAssoc.Checked == true)
            {

            }
        }
    }
}
